
import java.sql.*;
import java.util.HashMap;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author bettke
 */
public class Authentification {
    private Connection cx;
    private HashMap<String,String> hm; 
    
    public Authentification(String driver, String login, String mdp,String url) {
        try {
            Class.forName(driver);
            }
        catch( Exception ex ) {
            System.err.println( "Erreur lors du chargement du driver" );
            System.exit(1);
        }
        try {
            /** Connection à la base - Étape 2 */
            cx = DriverManager.getConnection (url,login,mdp);
            hm = new HashMap<String, String>();
        }
        catch( SQLException ex ) {
                System.err.println("Erreur lors de la cx à la base"+ex);
                System.exit(1);
        }
    }
    
    public void chargerBase(){
        try{
            Statement st = cx.createStatement();
            ResultSet rs = st.executeQuery("select * from LogPwd");
            
            while(rs.next())
                hm.put(rs.getString("login"),rs.getString("pwd"));
            st.close();
        }
        catch( SQLException ex ){
             System.err.println("Erreur lors de la cx à la base"+ex);
                System.exit(1);
        }
    }
    
    public boolean estReconnu(String login,String mdp){
        if(hm.get(login)!=null)
            return mdp.equalsIgnoreCase(hm.get(login));
        else
            return false;
    }
    
}
